<?php
namespace app\service;

use app\model\OrderModel;
use app\model\OrderRefundModel;
use EasyWeChat\Factory;
use support\facade\Logger;
use const support\CORRECT;
use const support\order_status_1;

class OrderRefundService{

    //订单退款根据微信订单号
    public static function toOrderRefund(int $memberId, string $refundNo): bool
    {
        try {
            //对用户订单状态进行查询
            $orderRefund=OrderRefundModel::where([
                'member_id'         =>$memberId,
                'order_refund_sn'   =>$refundNo,
                'order_refund_status'=>0
            ])->first();
            if (empty($orderRefund->id)){
                Logger::app('订单退款：无效退款订单','info',[$refundNo]);
                return false;
            }
            //获取订单交易流水号
            $order=OrderModel::where([
                'id'        =>$orderRefund->order_id,
                'order_status'=>order_status_1
            ])->select(['id','order_transaction_id','order_trade_no','goods_id','goods_period_id','goods_num'])->first();
            if (empty($order->id)){
                Logger::app('订单退款：无效订单','info',[$orderRefund->order_id]);
                return false;
            }
            $totalFee=$orderRefund->order_receipt_amount*100;
            $refundFee=$orderRefund->order_refund_amount*100;
            $config=config('wechat.wechat_pay');
            Logger::app('订单退款：微信小程序支付配置','info',$config);
            //初始化配置
            $app = Factory::payment($config);
            $result = $app->refund->byTransactionId($order->order_transaction_id, $refundNo, $totalFee, $refundFee, [
                'notify_url'  =>sprintf('%s/callback/wechatOrderRefundNotice/%s',env('APP_URL'),$refundNo),
                'refund_desc' => $orderRefund->order_refund_desc?:'退款',
            ]);
            Logger::app('订单退款：退款请求结果','info',$result);
            if (empty($result['result_code'])){
                return false;
            }
            if ($result['result_code']=='FAIL'){
                $update=[
                    'fail_reason'           =>$result['err_code_des']??'',
                    'order_refund_status'   =>3,
                    'update_time'           =>time()
                ];
            }else{
                $update=[
                    'order_refund_status'   =>CORRECT,
                    'update_time'           =>time()
                ];
            }
            $update['refund_id']=$result['refund_id']??'';
            $update['handle_time']=time();
            $ret=OrderRefundModel::where([
                'id'=>$orderRefund->id
            ])->update($update);
            Logger::app('订单退款：退款请求结果','info',[$ret]);
            if (!$ret){
                return false;
            }
            //进行库存表更
            $ret=OrderService::addStockNum($order->goods_id,$order->goods_period_id,$order->goods_num);
            Logger::app('订单退款：库存表更','info',[$ret]);
            return true;
        }catch (\Throwable $e){
            Logger::app($e);
            return false;
        }

    }

}